<?

// MySQLi database interface
// v 0.47
// not ready for prime time

class DB extends ChurroDB{
	
	// escape a single string by reference
	public function escape( &$x ){
		$x = mysqli_real_escape_string( $x, $this->connection );
	}
	
	// also called in constructor
	public function connectDB( $host, $username, $password  ){
		$this->connection = mysqli_connect( $host , $username, $password );
		if( $this->connection ){
			$this->host = $host;
			$this->username = $username;
			$this->password = $password;
			return TRUE;
		}
		
		$this->error = mysqli_connect_error();
		return FALSE;
	}
	
	// also called in constructor
	public function selectDB( $dbname ){
		$success = mysqli_select_db( $this->connection, $dbname );
		if( $success ){
			$this->dbname = $dbname;
			return TRUE;
		}
		
		$this->error = mysqli_error( $this->connection );
		return FALSE;
	}
	
	//executes a sql command from rows() or row() and returns result as array
	protected function execute( $sql, $key = null ){
		$return = array();
		$this->query = $sql;
		
		$result = mysqli_query( $this->connection, $sql );
		$this->queryCount ++;
		
		while( $row = mysqli_fetch_assoc($result) ){
			$r = array();
			
			foreach( $row as $k=>$v ) $r[$k] = $v;
			
			if( $key ){ 
				$return[ $row[$key] ] = (object) $r;
			} else {
				$return[] = (object) $r;
			}
		}
		
		return $return;
	}
}

// end of file
// class_DB_MySQLi.php